package com.car.service.impl;

import com.car.mapper.EmpMapper;
import com.car.mapper.MaintenanceAppointmentMapper;
import com.car.pojo.Emp;
import com.car.pojo.EmpQueryParam;
import com.car.pojo.MaintenanceAppointment;
import com.car.pojo.MaintenanceAppointmentQueryParam;
import com.car.pojo.PageResult;
import com.car.service.MaintenanceAppointmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 保养预约Service实现类
 */
@Service
public class MaintenanceAppointmentServiceImpl implements MaintenanceAppointmentService {
    
    @Autowired
    private MaintenanceAppointmentMapper maintenanceAppointmentMapper;
    
    @Autowired
    private EmpMapper empMapper;
    
    @Override
    public PageResult<MaintenanceAppointment> page(MaintenanceAppointmentQueryParam param) {
        // 验证页码，确保至少为1
        int pageNum = param.getPage() < 1 ? 1 : param.getPage();
        // 计算起始索引
        param.setPage((pageNum - 1) * param.getPageSize());
        
        // 查询数据
        List<MaintenanceAppointment> list = maintenanceAppointmentMapper.page(param);
        
        // 查询总记录数
        Long total = maintenanceAppointmentMapper.count(param);
        
        // 返回分页结果
        return new PageResult<>(total, list);
    }
    
    @Override
    public MaintenanceAppointment getInfo(Long id) {
        return maintenanceAppointmentMapper.getById(id);
    }
    
    @Override
    public void updateStatus(Long id, String status) {
        maintenanceAppointmentMapper.updateStatus(id, status, null);
    }
    
    @Override
    public void updateStatus(Long id, String status, String empName) {
        // 更新预约状态和保养员信息
        maintenanceAppointmentMapper.updateStatus(id, status, empName);
        
        // 如果分配了保养员，将该保养员的状态更新为"忙碌"
        if (empName != null && !empName.isEmpty()) {
            // 查询该保养员的信息
            EmpQueryParam empQueryParam = new EmpQueryParam();
            empQueryParam.setName(empName);
            List<Emp> empList = empMapper.list(empQueryParam);
            
            if (empList != null && !empList.isEmpty()) {
                Emp emp = empList.get(0);
                emp.setStatus("忙碌");
                empMapper.updateById(emp);
            }
        }
    }
    
    @Override
    public void delete(List<Long> ids) {
        maintenanceAppointmentMapper.deleteBatch(ids);
    }
    
    @Override
    public List<MaintenanceAppointment> list(MaintenanceAppointmentQueryParam param) {
        return maintenanceAppointmentMapper.listAll(param);
    }
    
    @Override
    public void add(MaintenanceAppointment appointment) {
        // 设置默认状态为待确认
        appointment.setStatus("待确认");
        // 添加预约
        maintenanceAppointmentMapper.add(appointment);
    }
    
    @Override
    public List<MaintenanceAppointment> listByUserId(Long userId) {
        return maintenanceAppointmentMapper.listByUserId(userId);
    }
    
    @Override
    public Integer countByUserId(Long userId) {
        return maintenanceAppointmentMapper.countByUserId(userId);
    }
    
    @Override
    public List<MaintenanceAppointment> listByEmpName(String empName) {
        return maintenanceAppointmentMapper.listByEmpName(empName);
    }
    
    @Override
    public List<Map<String, Object>> getMaintenanceStaff() {
        return maintenanceAppointmentMapper.getMaintenanceStaff();
    }
    
    @Override
    public Integer countConfirmedByEmpName(String empName) {
        return maintenanceAppointmentMapper.countConfirmedByEmpName(empName);
    }
    
    @Override
    public Integer countCompletedByEmpName(String empName) {
        return maintenanceAppointmentMapper.countCompletedByEmpName(empName);
    }
}